package com.patmen.android.PhoneCard;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * This class defines the ListAdapter for each profile item in the ListActivity.
 */
class ProfileListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;

	private String[] profilesName = new String[10];
	private Boolean[] profilesActive = new Boolean[10];
	private int profileCnt;

	public ProfileListAdapter(Context context) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		profileCnt = 0;
	}

	public void addItem(String profileName, boolean profileActive) {
		profilesName[profileCnt] = profileName;
		profilesActive[profileCnt] = profileActive;
		profileCnt++;
	}

	public int getCount() {
		return profileCnt;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	/**
	 * Make a view to hold each row.
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater
					.inflate(R.layout.profileslist_profile, null);

			holder = new ViewHolder();
			holder.profileName = (TextView) convertView
					.findViewById(R.id.profileName);
			holder.profileActive = (ToggleButton) convertView
					.findViewById(R.id.profileActive);

			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}

		// Bind the data efficiently with the holder.
		holder.profileName.setText(profilesName[position]);
		holder.profileName.setId(position * 2);
		holder.profileActive.setChecked(profilesActive[position]);
		holder.profileActive.setId(position * 2 + 1);

		// Add click listener for the profile name
		holder.profileName.setOnLongClickListener(new OnLongClickListener() {
			public boolean onLongClick(View v) {
				((View)(v.getParent())).performLongClick();
				return true;
			}
		});

		holder.profileName.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						com.patmen.android.PhoneCard.ProfileEditor.class);
				intent.putExtra("ProfileIndex", v.getId() / 2);

				v.getContext().startActivity(intent);
			}
		});


		// Add click listener for the profileActive
		holder.profileActive.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SharedPreferences settings = v.getContext()
						.getSharedPreferences(PhoneCard.PREFS_NAME, 0);
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("Profile" + (v.getId() - 1) / 2 + "Active",
						((ToggleButton) v).isChecked());
				editor.commit();
			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView profileName;
		ToggleButton profileActive;
	}
}
